package vashion.azeroth.core.verificationcode.sms;

import java.util.Map;

import org.apache.http.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chuanglan.port.HttpSender;
import com.google.common.collect.Maps;

public class ChuanglanSMSSender {

	private final static Logger logger = LoggerFactory.getLogger(ChuanglanSMSSender.class);

	private String internationalUrl;
	
	private String internationalAccount;
	
	private String internationalPassword;

	/**
	 * 发送验证短信
	 * 
	 * @param mobilePhone
	 * @param message
	 * @return
	 * @throws Exception
	 */
	public boolean sendVerificationCodeSMS(String mobilePhone, String message) throws Exception {
		String url = "http://sms.253.com/msg/";// 应用地址
		String un = "N1638215";// 账号
		String pw = "vKCqVN0tQZa391";// 密码
		String phone = mobilePhone;// 手机号码，多个号码使用","分割
		String msg = message;// 发送的短信内容
		String rd = "1";// 是否需要状态报告，需要1，不需要0
		String ex = null;// 扩展码
		Map<String, String> nameValueMap = Maps.<String, String>newHashMap();
		nameValueMap.put("un", un);
		nameValueMap.put("pw", pw);
		nameValueMap.put("phone", phone);
		nameValueMap.put("rd", rd);
		nameValueMap.put("msg", msg);
		nameValueMap.put("ex", ex);
		try {
			String returnMessage = HttpSender.batchSend(url, nameValueMap);
			System.out.println(returnMessage);
			int codeStart = returnMessage.indexOf(',');
			int codeEnd = returnMessage.length();
			if (returnMessage.indexOf('\n') > codeStart) {
				codeEnd = returnMessage.indexOf('\n');
			}
			String statusCode = returnMessage.substring(codeStart + 1, codeEnd);
			if ("0".equals(statusCode)) {
				return true;
			}
		} catch (Exception e) {
			logger.error("发送验证码短信异常", e);
			throw e;
		}
		return false;
	}

	public boolean sendInternationVerificationCodeSMS(String mobilePhone, String message) throws Exception {
		HttpSender client = new HttpSender(internationalUrl, internationalAccount, internationalPassword);
		String result = null;
		try {
			// 发送国际验证码 发送手机号（国家区号+手机号码）
			result = client.sendInternationalMessage(mobilePhone, message);

		} catch (ParseException e) {
			e.printStackTrace();
		}
		client.close();

		if (null != result && result.contains("true")) {
			return true;
		} else {
			return false;
		}
	}

	public String getInternationalUrl() {
		return internationalUrl;
	}

	public void setInternationalUrl(String internationalUrl) {
		this.internationalUrl = internationalUrl;
	}

	public String getInternationalAccount() {
		return internationalAccount;
	}

	public void setInternationalAccount(String internationalAccount) {
		this.internationalAccount = internationalAccount;
	}

	public String getInternationalPassword() {
		return internationalPassword;
	}

	public void setInternationalPassword(String internationalPassword) {
		this.internationalPassword = internationalPassword;
	}
	
}
